package com.summer.order.service.impl;

import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.summer.order.model.Cookbook;
import com.summer.order.model.DayFood;
import com.summer.order.service.CookbookService;

@Service("cookbookService")
public class CookbookServiceImpl implements CookbookService {

	@Resource
	HibernateTemplate ht;
	
	@Override
	public Cookbook getLastCookbook() {
		// TODO Auto-generated method stub
		Cookbook cookbook = null;
		List<Cookbook> cookbookList = ht.find("from Cookbook cookbook order by cookbook.startDate desc");
		if(cookbookList != null && cookbookList.size() != 0) {
			cookbook = cookbookList.get(0);
			List<DayFood> dayFoods = ht.find("from DayFood dayFood where dayFood.cookbook.id=?",cookbook.getId());
			cookbook.setDayFoods(new HashSet<DayFood>(dayFoods));
		}
		return cookbook;
	}
	
	public List<DayFood> getDayFoodsByCookbook(Cookbook cookbook) {
		List<DayFood> dayFoods = null;
		if(cookbook != null) {
			dayFoods = ht.find("from DayFood dayFood where dayFood.cookbook.id=?",cookbook.getId());
		}
		
		
		return dayFoods;
	}

}
